/*global desc, task, jake, fail, complete */
(function() {
    "use strict";

    task("default", ["lint", "test"], function() {
        console.log("\n\nOK");
    });

    desc("Lint everything");
    task("lint", [], function() {
        var lint = require("./build/lint/lint_runner.js");

        var files = new jake.FileList();
        files.include("**/*.js");
        files.exclude("karma.conf.js");
        files.exclude("node_modules");
        files.exclude("src/js/vendor/**");
        files.exclude("src/js-tests/**");
        var options = nodeLintOptions();
        var passed = lint.validateFileList(files.toArray(), options, {});

        if (!passed) fail("Lint failed");
    });

    desc("Test client code");
    task("test", function() {
        var config = {};

        var output = "";
        var oldStdout = process.stdout.write;
        process.stdout.write = function(data) {
            output += data;
            oldStdout.apply(this, arguments);
        };

        require("karma/lib/runner").run(config, function(exitCode) {
            process.stdout.write = oldStdout;

            if (exitCode) fail("Client tests failed (to start server, run 'jake karma')");
//            var browserMissing = false;
//            SUPPORTED_BROWSERS.forEach(function(browser) {
//                browserMissing = checkIfBrowserTested(browser, output) || browserMissing;
//            });
//            if (browserMissing && !process.env.loose) fail("Did not test all supported browsers (use 'loose=true' to suppress error)");
            if (output.indexOf("TOTAL: 0 SUCCESS") !== -1) fail("Client tests did not run!");

            complete();
        });
    }, {async: true});

    function nodeLintOptions() {
        return {
            bitwise:true,
            curly:false,
            eqeqeq:true,
            forin:true,
            immed:true,
            latedef:true,
            newcap:true,
            noarg:true,
            noempty:true,
            nonew:true,
            regexp:true,
            undef:true,
            strict:true,
            trailing:true,
            node:true
        };
    }
}());